package com.qmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmq.entity.Memorandum;
import com.qmq.mapper.MemorandumMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author modify
 */
@RestController
@CrossOrigin
@Slf4j
public class MemorandumController {
    @Autowired
    private MemorandumMapper memorandumMapper;

    @PostMapping("/memorandum/getPage")
    @ApiOperation(value = "分页获取待办信息", notes = "/memorandum/getPage")
    public IPage<Memorandum> getPageMemorandum(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "Memorandum", value = "Memorandum", required = true) Memorandum memorandum) {
        QueryWrapper queryWrapper = new QueryWrapper();
        Page<Memorandum> studentInfoPage = new Page<>(pageNum, pageSize);
        return memorandumMapper.selectPage(studentInfoPage, queryWrapper);
    }

    @PostMapping("/memorandum/update")
    @ApiOperation(value = "新增、修改待办信息", notes = "/memorandum/update")
    public Integer updateMemorandum(@RequestBody @ApiParam(name = "Memorandum", value = "Memorandum", required = true) Memorandum memorandum) {
        if (memorandum.getId() != null) {
            return memorandumMapper.updateById(memorandum);
        } else {
            return memorandumMapper.insert(memorandum);
        }
    }

    @PostMapping("/memorandum/delete")
    @ApiOperation(value = "删除待办信息", notes = "/memorandum/delete")
    public Integer deleteMemorandum(@RequestParam @ApiParam(name = "id", value = "id", required = true) int id) {
        return memorandumMapper.deleteById(id);
    }

    @GetMapping("/memorandum/getDetailById")
    @ApiOperation(value = "根据id获取", notes = "/memorandum/getDetailById")
    public Memorandum getDetailById(@RequestParam @ApiParam(name = "id", value = "id", required = true) int id) {
        return memorandumMapper.selectById(id);
    }

    /**
     * 模拟SSE serve send event
     * */
    @GetMapping(value = "/sse")
    public String sse(HttpServletResponse response) {
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("utf-8");
        String s = "";
        while (true) {
            s = "data: " + new Date() + "\n\n";
            try {
                PrintWriter pw = response.getWriter();
                Thread.sleep(1000);
                pw.write(s);
                pw.flush();
            } catch (Exception e) {

            }
        }
    }
}
